package com.wzgl.core.webmagic.pipeline;

import com.jfinal.aop.Duang;
import com.wzgl.core.CacheConsts;
import com.wzgl.core.model.Tgrid;
import com.wzgl.core.server.GirdService;
import com.wzgl.core.utils.WZGLUtils;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

public class TgridsImgPipeLine implements Pipeline{
	GirdService service = Duang.duang(GirdService.class);
	String pic_1 = "http://www.27270.com/";
	
	@Override
	public void process(ResultItems  r, Task arg1) {
		// TODO Auto-generated method stub
		try {
			String herf =r.getRequest().getUrl();
			if(herf != null){
				if(!herf.equals(pic_1)){
					Tgrid t = (Tgrid) WZGLUtils.getCache(CacheConsts.CACHE_MAGIC_GIRD_NAME, herf);
					boolean b = service.saveMagicTgridsImg(t);
					if(b == false || t == null){
						System.out.println(herf+": 保存本地数据库失败");
					}
					String url = jugeUrl(herf);
					String log = (String) WZGLUtils.getCache(CacheConsts.CACHE_LOG_MAGIC_NAME, url);
			   		log += "保存URL:"+herf+"数据.....";
			   		String log_cache = CacheConsts.CACHE_LOG_MAGIC_NAME;
	       		    String log_name = CacheConsts.CACHE_LOG_MAGIC_NAME+":"+url;
	       		    WZGLUtils.setCache(log_cache, log_name, log);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	private String jugeUrl(String herf) {
		String url = "";
		if(herf.contains("www.27270.com")){
			url = pic_1;
		}
		return url;
	}
}
